<?php

namespace Payment\Controller;

use Think\Controller;
use Think\Log;
use Payment\Notify\PayNotifyInterface;
use Payment\Config;
use Payment\Common\PayException;
use Payment\Client\Notify;
//use JiaLeo\Payment\Unionpay\Notify as unionpayNotify;
//use JiaLeo\Payment\Unionpay\BaseUnionpay as BaseUnionpay;
use Think\Controller\RestController;

//use Util\PaypalIpn;

class CallbackController extends RestController {
    /*
     * 微信支付
     */

    public function wxpay() {
        $logFile = LOG_PATH . 'Payment/' . date('Y-m-d') . '_payment.log';
        $type = Config::WX_CHARGE;
        $callback = new PayNotify();
        $data = file_get_contents('php://input');
        Log::write(json_encode($data), Log::INFO, '', $logFile);
        $config = C('sdk.wxpay');
        $ret = Notify::run($type, $config, $callback);
        return $ret;
    }

    /*
     * 微信h5支付
     */

    public function wxpay_pub() {
        $logFile = LOG_PATH . 'Payment/' . date('Y-m-d') . '_payment.log';
        $type = Config::WX_CHARGE;
        $callback = new PayNotify();
        $data = file_get_contents('php://input');
        Log::write(json_encode($data), Log::INFO, '', $logFile);
        $config = C('sdk.wxpay_pub');
        $ret = Notify::run($type, $config, $callback);
        return $ret;
    }

    /*
     * 阿里支付
     */

    public function alipay() {
        $logFile = LOG_PATH . 'Payment/' . date('Y-m-d') . '_payment.log';
        $type = Config::ALI_CHARGE;
        $callback = new PayNotify();
        $data = file_get_contents('php://input');
        Log::write(json_encode($data), Log::INFO, '', $logFile);
        $config = C('sdk.alipay');
        $ret = Notify::run($type, $config, $callback);
        return $ret;
    }

    public function applepay() {
        $logFile = LOG_PATH . 'Payment/' . date('Y-m-d') . '_payment.log';
        $data = file_get_contents('php://input');
        Log::write(json_encode($data), Log::INFO, '', $logFile);
    }

    /*
     * 银联支付
     */

    public function unionpay() {
        $logFile = LOG_PATH . 'Payment/' . date('Y-m-d') . '_payment.log';
        if (empty(I('post.')) && empty(I('get.'))) {
            Log::record('params is empty', 'info', TRUE);
            die();
        }
        $data = I('post.') ? : I('get.');
        if (empty($data)) {
            Log::record('params is empty', 'info', TRUE);
            die();
        }
//        $data = json_decode('{"accNo":"6216***********0018","accessType":"0","bizType":"000201","certId":"69026276696","currencyCode":"156","encoding":"utf-8","merId":"777290058110048","orderId":"PAY482355970704","payCardType":"01","payType":"0001","queryId":"111907041123456813758","respCode":"00","respMsg":"Success!","settleAmt":"200","settleCurrencyCode":"156","settleDate":"0704","signMethod":"01","traceNo":"681375","traceTime":"0704112345","txnAmt":"200","txnSubType":"01","txnTime":"20190704112345","txnType":"01","version":"5.0.0","signature":"q+b6DnkGB8KboHzpMJvC7L8PjJRUBe8hLsSFZKBd250c0qjKWIZb5BquptjkS3\/GSEZhglj11RPNcrtOwULkBfple2VScHIdILTatoR4NTHj9I5zD9XRXcaWfRQUkuVd3E5jmzHCmNMHFc8q+lGbZgnYpioWTmgI8JOkJrjexPy9eD8soYGBsy9q8iIEqmx7epAPyclQSD1om1StxM7F3jktuY7RAyZhxt8d3\/gAEZiBE0h7dnzBxNKGXTuYn+n12u0xtil5qHqOyfE5+7MlOnEOz\/QPTa8jemDf1sWTkCecrWgAjEL5CnxqO4FefbPKQ\/6L8cK5hSVLQxu3CY3YvA=="}',TRUE);
//        Log::record(json_encode($data), 'info', TRUE);
        $config = C('sdk.unionpay');
        $notify = new \JiaLeo\Payment\Unionpay\Notify();
        $ret = $notify->handle($data,$config);
        if (in_array($ret['respCode'], ['00', 'A6'])) {
            $chanrge = D('Payment');
            $ret = $chanrge->deal($ret);
        }
        return $ret;
    }

}

class PayNotify implements PayNotifyInterface {

    public function notifyProcess(array $data) {
        $ret = false;
        $logFile = LOG_PATH . 'Payment/' . date('Y-m-d') . '_notify.log';
        Log::write(json_encode($data), Log::INFO, '', $logFile);
        $channel = $data['channel'];
        $chanrge = D('Chanrge');
        if ($channel === Config::ALI_CHARGE) {// 支付宝支付
            $ret = $chanrge->deal($data);
        } elseif ($channel === Config::WX_CHARGE) {// 微信支付
            $ret = $chanrge->deal($data);
        } elseif ($channel === Config::CMB_CHARGE) {// 招商支付
        } elseif ($channel === Config::CMB_BIND) {// 招商签约
        } elseif ($channel === Config::WX_CHANNEL_APP || $channel == Config::WX_CHANNEL_QR) {
            $ret = $chanrge->deal($data);
        } else {
            
        }
        return $ret;
    }

}
